<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 14-5-8
 * Time: 下午11:40
 */

class Madvertisepic extends CI_Model{
    function __construct(){
        parent::__construct();
    }

    function getAdvertisePic($id)
    {
        $sql = "select * from `tbadvertise_pic` where `advertise_id`=?";
        $query = $this->db->query($sql,array($id));
        return $row = $query->result();

    }

    function addAdvertisePic($advertise_id,$pic_path,$sort,$ulr)
    {
        /*$advertisePic = $this->getAdvertisePic($advertise_id);
        $dirname = dirname(__FILE__);
        $dir = $dirname.'/../../uploads/';
        foreach($advertisePic as $row){
            if(!empty($row->pic_path)){
                $filePath =  $dir.$row->pic_path;
                if(is_file($filePath)){
                    @unlink($filePath);
                }
            }
        }*/
        $this->deleteAdvertisePic($advertise_id);
        if(!empty($pic_path)){
            foreach($pic_path as $key=>$value){
                $sortId = isset($sort[$key]) && !empty($sort[$key]) ? $sort[$key] :0;
                $sqlArr[] = "('$value',$advertise_id,$sortId,'$ulr')";
            }
            $sql = "INSERT INTO `tbadvertise_pic`(`pic_path`,`advertise_id`,`sort`,`url`)VALUES".implode(',',$sqlArr);
            $this->db->query($sql);
        }
        return $this->db->insert_id();
    }

    function deleteAdvertisePic($advertise_id)
    {
        $this->db->delete('tbadvertise_pic', array('advertise_id' => $advertise_id));
    }

}